<?php

/**
 * @version     1.5
 * @package	EP EasyPanel
 * @copyright	Copyright (C) 2010 LBN Studio. All rights reserved.
 * @license     BSD, see LICENSE.TXT
 * @link        http://www.labolsanegra.org/
 */
// Acceso restringido
defined('JL') or die('Acceso denegado');

/**
 * JLImage
 * Handler for images types: jpg, png and gif
 * @package     EP EasyPanel
 * @author      Jose Luis Quintana
 * @version     1.0 
 * @copyright	Copyright (C) 2010 LBN Studio. All rights reserved
 * @access      public
 */
class JLImage {

    var $image;
    var $imageType;
    var $mimeType;
    var $originalFileName;

    /**
     * JLImage::JLImage()
     * contructor from class JLImage.
     * @param string $fileNameImage
     * @return void
     */
    function JLImage($filepath = null) {
        if (!is_null($filepath)) {
            $this->load($filepath);
        }
    }

    /**
     * JLImage::load()
     * Load an image jpg, png or gif to manipulate.
     * @param string $fileNameImage
     * @return void
     */
    function load($fileNameImage) {
        try {
            if (!is_file($fileNameImage)) {
                throw new Exception('The file path image "' . $fileNameImage . '" not exist.');
            }

            $image_info = getimagesize($fileNameImage);
            $type = $image_info[2];
            $this->mimeType = $image_info[4];

            switch ($type) {
                case IMAGETYPE_JPEG:
                    $image = imagecreatefromjpeg($fileNameImage);
                    break;
                case IMAGETYPE_GIF:
                    $image = imagecreatefromgif($fileNameImage);
                    break;
                case IMAGETYPE_PNG:
                    $image = imagecreatefrompng($fileNameImage);
                    break;
                default:
                    throw new Exception('The image format is not valid jpg, png or gif.');
                    break;
            }

            $this->originalFileName = $fileNameImage;
            $this->imageType = $type;
            $this->image = $image;
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * JLImage::save()
     * Save image to filepath.
     * @param string $fileNameImage
     * @param int $quality
     * @param bool $destroyResource
     * @return bool
     */
    function save($fileNameImage = null, $quality = 75, $destroyResource = true) {
        if (!$fileNameImage) {
            $fileNameImage = $this->originalFileName;
        }

        $imageType = $this->imageType;

        switch ($imageType) {
            case IMAGETYPE_JPEG:
                $ok = imagejpeg($this->image, $fileNameImage, $quality);
                break;
            case IMAGETYPE_GIF:
                $ok = imagegif($this->image, $fileNameImage);
                break;
            case IMAGETYPE_PNG:
                $ok = imagepng($this->image, $fileNameImage, $quality);
                break;
        }

        if ($destroyResource) {
            $this->imageDestroy();
        }

        return $ok;
    }

    /**
     * JLImage::getType()
     * Get type of image.
     * @return int
     */
    function getType() {
        return $this->imageType;
    }

    /**
     * JLImage::display()
     * Display a image in browser.
     * @param int $quality
     * @param bool $destroyResource
     * @return bool
     */
    function display($quality = 75, $destroyResource = true) {
        $imageType = $this->imageType;
        header("Content-type: " . $this->mimeType);

        switch ($imageType) {
            case IMAGETYPE_JPEG:
                $ok = imagejpeg($this->image, null, $quality);
                break;
            case IMAGETYPE_GIF:
                $ok = imagegif($this->image, null);
                break;
            case IMAGETYPE_PNG:
                $ok = imagepng($this->image, null, $quality);
                break;
        }

        if ($destroyResource) {
            $this->imageDestroy();
        }

        return $ok;
    }

    /**
     * JLImage::getResource()
     * Get resource form image loaded.
     * @return resource
     */
    function getResource() {
        return $this->image;
    }

    /**
     * JLImage::getWidth()
     * Get width form image.
     * @return int
     */
    function getWidth() {
        return imagesx($this->image);
    }

    /**
     * JLImage::getHeight()
     * Get height form image.
     * @return int
     */
    function getHeight() {
        return imagesy($this->image);
    }

    /**
     * JLImage::resizeToHeight()
     * Resize proportional image form height.
     * @param int $height
     * @return void
     */
    function resizeToHeight($height) {
        $ratio = (int) $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        $this->resize($width, $height);
    }

    /**
     * JLImage::resizeToWidth()
     * Resize proportional image form width.
     * @param int $width
     * @return void
     */
    function resizeToWidth($width) {
        $ratio = (int) $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        $this->resize($width, $height);
    }

    /**
     * JLImage::scale()
     * Scale a image.
     * @param int $scale
     * @return void
     */
    function scale($scale) {
        $width = $this->getWidth() * (int) $scale / 100;
        $height = $this->getheight() * (int) $scale / 100;
        $this->resize($width, $height);
    }

    /**
     * JLImage::crop()
     * Crop an image.
     * @param int $widthCrop
     * @param int $heightCrop
     * @param int $x1
     * @param int $y1
     * @param int $dst_x
     * @param int $dst_y
     * @return void
     */
    function crop($widthCrop, $heightCrop, $x1 = 0, $y1 = 0, $dst_x = 0, $dst_y = 0) {
        $this->resize($widthCrop, $heightCrop, $x1, $y1, $dst_x, $dst_y, true);
    }

    /**
     * JLImage::resize()
     * Resize to image.
     * @param int $width
     * @param int $height
     * @param int $x1
     * @param int $y1
     * @param int $dst_x
     * @param int $dst_y
     * @param bool $isCrop
     * @return void
     */
    private function resize($width, $height, $x1 = 0, $y1 = 0, $dst_x = 0, $dst_y = 0, $isCrop = false) {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, $dst_x, $dst_y, $x1, $y1, $width, $height, ($isCrop) ? $width : $this->getWidth(), ($isCrop) ? $height : $this->getHeight());
        imagedestroy($this->image);
        $this->image = $new_image;
    }

    /**
     * Image::imageDestroy()
     * Destroy an image resource.
     * @return bool
     */
    function imageDestroy() {
        $image = $this->image;
        $this->image = null;
        return (is_null($image)) ? false : imagedestroy($image);
    }

}